package com.ruoyi.knowledge.service.impl;

import java.util.List;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.knowledge.mapper.QualityDiseasesCatalogueMapper;
import com.ruoyi.knowledge.domain.QualityDiseasesCatalogue;
import com.ruoyi.knowledge.service.IQualityDiseasesCatalogueService;

/**
 * 常见质量通病目录Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-07-14
 */
@Service
public class QualityDiseasesCatalogueServiceImpl implements IQualityDiseasesCatalogueService 
{
    @Autowired
    private QualityDiseasesCatalogueMapper qualityDiseasesCatalogueMapper;

    /**
     * 查询常见质量通病目录
     * 
     * @param catalogueId 常见质量通病目录主键
     * @return 常见质量通病目录
     */
    @Override
    public QualityDiseasesCatalogue selectQualityDiseasesCatalogueByCatalogueId(Long catalogueId)
    {
        return qualityDiseasesCatalogueMapper.selectQualityDiseasesCatalogueByCatalogueId(catalogueId);
    }

    /**
     * 查询常见质量通病目录列表
     * 
     * @param qualityDiseasesCatalogue 常见质量通病目录
     * @return 常见质量通病目录
     */
    @Override
    public List<QualityDiseasesCatalogue> selectQualityDiseasesCatalogueList(QualityDiseasesCatalogue qualityDiseasesCatalogue)
    {
        return qualityDiseasesCatalogueMapper.selectQualityDiseasesCatalogueList(qualityDiseasesCatalogue);
    }

    /**
     * 新增常见质量通病目录
     * 
     * @param qualityDiseasesCatalogue 常见质量通病目录
     * @return 结果
     */
    @Override
    public int insertQualityDiseasesCatalogue(QualityDiseasesCatalogue qualityDiseasesCatalogue)
    {
        return qualityDiseasesCatalogueMapper.insertQualityDiseasesCatalogue(qualityDiseasesCatalogue);
    }

    /**
     * 修改常见质量通病目录
     * 
     * @param qualityDiseasesCatalogue 常见质量通病目录
     * @return 结果
     */
    @Override
    public int updateQualityDiseasesCatalogue(QualityDiseasesCatalogue qualityDiseasesCatalogue)
    {
        return qualityDiseasesCatalogueMapper.updateQualityDiseasesCatalogue(qualityDiseasesCatalogue);
    }

    /**
     * 批量删除常见质量通病目录
     * 
     * @param catalogueIds 需要删除的常见质量通病目录主键
     * @return 结果
     */
    @Override
    public int deleteQualityDiseasesCatalogueByCatalogueIds(Long[] catalogueIds)
    {
        return qualityDiseasesCatalogueMapper.deleteQualityDiseasesCatalogueByCatalogueIds(catalogueIds);
    }

    /**
     * 删除常见质量通病目录信息
     * 
     * @param catalogueId 常见质量通病目录主键
     * @return 结果
     */
    @Override
    public int deleteQualityDiseasesCatalogueByCatalogueId(Long catalogueId)
    {
        return qualityDiseasesCatalogueMapper.deleteQualityDiseasesCatalogueByCatalogueId(catalogueId);
    }

    @Override
    public QualityDiseasesCatalogue getTree() {
     List<QualityDiseasesCatalogue> list =   qualityDiseasesCatalogueMapper.selectAll();
     //封装一个根节点
        QualityDiseasesCatalogue root = new QualityDiseasesCatalogue();
        root.setCatalogueId(0L);
        root.setCatalogueName("根节点");
        root.setCataloguePid(0L);
        //给根节点添加子节点
        addChild(root,list);
        return root;
    }

    @Override
    public Long queryIdByPid(Long catalogueId) {
        return qualityDiseasesCatalogueMapper.queryIdByPid(catalogueId);
    }

    @Override
    public Long queryIdByPid1(Long catalogueId) {
        return qualityDiseasesCatalogueMapper.queryIdByPid1(catalogueId);
    }



    private void addChild(QualityDiseasesCatalogue root, List<QualityDiseasesCatalogue> list) {
        //找到子节点
        List<QualityDiseasesCatalogue> children = list.stream()
                //用根节点的Id 对应 子节点的 Pid(上级目录)
                .filter(qdc -> Objects.equals(root.getCatalogueId(), qdc.getCataloguePid()))
                //给子节点在加上子节点    一级传进去,给一级子节点加上二级子节点
                .peek(qdc -> addChild(qdc, list))
                .toList();
        //将子节点集合放入根节点
        root.setChildren(children);
    }
}
